package com.android.pandoid.service;

import android.content.Context;
import android.content.Intent;
import android.telecom.Call;
import android.telecom.InCallService;
import android.util.Log;

import com.android.pandoid.PhoneCallActivity;
import com.android.pandoid.help.ActivityStack;
import com.android.pandoid.help.PhoneCallManager;

/**
 * Date: 2020/8/26 15:34
 * Author: Tatum
 * Description:   PhoneCallService  ----->PhoneCallActivity
 */
public class PhoneCallService extends InCallService {
    private Call.Callback callback = new Call.Callback() {
        @Override
        public void onStateChanged(Call call, int state) {
            super.onStateChanged(call, state);
            switch (state) {
                case Call.STATE_CONNECTING: //呼出电话的初始状态。
                    Log.d("yi", "----PhoneCallService----Call.Callback   呼出电话的初始状态");
                    break;
                case Call.STATE_HOLDING: //处于保持状态时的呼叫状态
                    Log.d("yi", "----PhoneCallService----Call.Callback   处于保持状态时的呼叫状态");
                    break;
                case Call.STATE_NEW: //新建呼叫时的状态。
                    Log.d("yi", "----PhoneCallService----Call.Callback   新建呼叫时的状态");
                    break;
                case Call.STATE_PULLING_CALL: //正在从远程设备拉到本地设备的外部呼叫状态。。
                    Log.d("yi", "----PhoneCallService----Call.Callback   正在从远程设备拉到本地设备的外部呼叫状态");
                    break;
                case Call.STATE_RINGING:   //在本地响铃但尚未连接时来电的状态。
                    Log.d("yi", "----PhoneCallService----Call.Callback   响铃中");
                    break; // 响铃中
                case Call.STATE_ACTIVE:
                    Log.d("yi", "----PhoneCallService----Call.Callback   通话中");
                    break; // 通话中
                case Call.STATE_DISCONNECTED:
                    Log.d("yi", "----PhoneCallService----Call.Callback  通话结束");
                    ActivityStack.getInstance().finishActivity(PhoneCallActivity.class);
                    break; // 通话结束
            }
        }
    };

    @Override
    public void onCallAdded(Call call) {
        super.onCallAdded(call);
        Log.d("yi", "----PhoneCallService----onCallAdded   -----");
        call.registerCallback(callback);
        PhoneCallManager.call = call; // 传入call

        CallType callType = null;

        if (call.getState() == Call.STATE_RINGING) {
            callType = CallType.CALL_IN;
        } else if (call.getState() == Call.STATE_CONNECTING) {
            callType = CallType.CALL_OUT;
        }

        if (callType != null) {
            Call.Details details = call.getDetails();

            Log.d("yi", "----PhoneCallService----Call.Callback   响铃中--" + details.toString());
            Log.d("yi", "----PhoneCallService----Call.Callback   响铃中" + details.getHandle().getUserInfo());
            Log.d("yi", "----PhoneCallService----Call.Callback   响铃中" + details.getHandle().getAuthority());
            Log.d("yi", "----PhoneCallService----Call.Callback   响铃中" + details.getHandle().getPath());

            String phoneNumber = details.getHandle().getSchemeSpecificPart();
            Log.d("yi", "----PhoneCallService----Call.Callback   响铃中=====" + phoneNumber);
            actionStart(this, phoneNumber, callType);
        }
    }

    private void actionStart(Context context, String phoneNumber,
                             PhoneCallService.CallType callType) {
        Intent intent = new Intent(context, PhoneCallActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
        intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
        context.startActivity(intent);
    }

    @Override
    public void onCallRemoved(Call call) {
        super.onCallRemoved(call);
        Log.d("yi", "----PhoneCallService----onCallRemoved   -----");
        call.unregisterCallback(callback);
        PhoneCallManager.call = null;
    }

    public enum CallType {
        CALL_IN,
        CALL_OUT,
    }
}
